package com.fxl.majiang.controller;

import com.fxl.majiang.room.RoomManager;
import com.fxl.majiang.service.RoomService;
import com.fxl.majiang.service.UserService;
import com.fxl.majiang.vo.ApiResponse;
import com.fxl.majiang.vo.request.DistributionRoomRequestVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author fengxinglei
 * @date 2022/8/16 16:36
 * @description TODO
 */
@RestController
@RequestMapping("/room")
public class RoomController {

    @Resource
    private UserService userService;

    @Resource
    private RoomService roomService;

    @PostMapping("/distribution")
    public ApiResponse<String> distribution(@RequestBody DistributionRoomRequestVo vo) {
        String userId = vo.getUserId();
        if (!StringUtils.hasText(userId)) {
            return ApiResponse.error("请传递必要参数");
        }
        if (!userService.check(userId)) {
            return ApiResponse.error("id不存在");
        }
        String roomId = roomService.distribution(userId);
        if (StringUtils.hasText(roomId)) {
            return ApiResponse.success(roomId);
        }
        return ApiResponse.error("当前房间过于火爆，请稍后再试");
    }

    @GetMapping("/clearAllRooms")
    public ApiResponse<Boolean> clearAllRooms() {
        RoomManager.clearAllRooms();
        return ApiResponse.success(true);
    }
}
